home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Graphics / Misc / Wood.0.72 / Sources / MMA.bproj / parser.ym < prev    next >
Encoding:
Text File  |  1994-06-05  |  1.9 KB  |  122 lines

  1.  
  2. %{
  3.  
  4. #import <ansi/stdlib.h>
  5. #import <ansi/string.h>
  6. #import <ansi/stdio.h>
  7. #import <ansi/ctype.h>
  8. #import <ansi/math.h>
  9.  
  10. #import "../Tree.h"
  11.  
  12. extern void mmaPrepareTextToScan(NXStream *text);
  13. extern yylex(), yyparse();
  14.  
  15. static void yyerror(char *s);
  16. static NXZone *treeZone;
  17. static Properties *treeProps;
  18. static Tree *resultTree;
  19. static Tree *mmaMakeLeaf(char *label);
  20. static Tree *mmaMakeTree(char *label, List *children);
  21. static List *mmaAddChild(Tree *tree, List *children);
  22.  
  23. %}
  24.  
  25. %union{
  26.     Tree *tree;
  27.     char *name;
  28.     List *list;
  29. }
  30.  
  31. %token <name> NAME 
  32. %token COMMA
  33. %left LB RB
  34.  
  35. %type <tree> mmatree mmadoc
  36. %type <list> childrenlist
  37.  
  38. %%
  39.  
  40. mmadoc:        mmatree
  41.             {
  42.                 resultTree = $1;
  43.             }
  44.             ;
  45.             
  46. mmatree:    NAME
  47.             {
  48.                 $$ = mmaMakeLeaf($1);
  49.             }
  50.             | LB NAME COMMA childrenlist RB
  51.             {
  52.                 $$ = mmaMakeTree($2, $4);
  53.             }
  54.             | /* empty */
  55.             {
  56.                 $$ = nil;
  57.             }
  58.             ;
  59.  
  60. childrenlist:     mmatree
  61.             {     
  62.                 $$ = mmaAddChild($1, nil); 
  63.             }    
  64.             | mmatree COMMA childrenlist
  65.             {     
  66.                 $$ = mmaAddChild($1, $3); 
  67.             }
  68.             ;
  69.                 
  70. %%
  71.  
  72. static Tree *mmaMakeLeaf(char *label)
  73. {
  74.     return [[Tree allocFromZone:treeZone] initLabel:label props:treeProps];
  75. }
  76.  
  77. static Tree *mmaMakeTree(char *label, List *children)
  78. {     
  79.     Tree *tree, *child;
  80.     int i;
  81.     
  82.     tree = [[Tree allocFromZone:treeZone] initLabel:label props:treeProps];        
  83.     for(i = 0; i < [children count]; i++){
  84.         child = (Tree *)[children objectAt:i];
  85.         [tree addTree:child];
  86.     }
  87.     [children free];
  88.     return tree; 
  89. }
  90.  
  91. static List *mmaAddChild(Tree *tree, List *children)
  92. {
  93.     List *list;
  94.     
  95.     if(children){
  96.         [children addObject:tree];
  97.         return children;
  98.     } else {
  99.         list = [[List alloc] init];
  100.         [list addObject:tree];
  101.         return list;
  102.     }
  103. }
  104.  
  105. id createMMATree(NXStream *stream, Properties *props, NXZone *zone)
  106. {    
  107.     int r;
  108.  
  109.     resultTree = nil;
  110.     mmaPrepareTextToScan(stream);
  111.     treeZone = zone;
  112.     treeProps = props;
  113.     r = yyparse();
  114.     return resultTree;
  115. }
  116.     
  117. static void yyerror(char *s)
  118. {
  119.     printf("parser error:%s\n",s);
  120. }
  121.  
  122.